import Api from "../config/api";
import Config from "../config/common"
import { encrypt_3des } from '../utils/des'

/**
 * 获取UUID和秘钥
 */
export const getSecretKey = () => {
    return new Promise((resolve, reject) => {
        getApp().sendRequest({
            url: Api.GET_SECRETKEY,
            data:{
                method: "getSecretKey.do"
            },
            method: "POST",
            success(resData){
                if (Config.ERROR_OK == resData.data.code) {
                    resolve(resData.data.data)                
                }else {
                    reject(resData)
                }
            },
            fail(error){
                reject(error)
            }
        })
    })
}

/**
 * 登录
 */
export const login = (data) => {
    const handledData = Object.assign(data)
    handledData.password = encrypt_3des(handledData.userName + handledData.secretKey + handledData.loginType, handledData.password)
    delete handledData.secretKey
    return new Promise((resolve, reject) => {
        getApp().sendRequest({
            url: Api.LOGIN,
            data: handledData,
            method: "POST",
            success(resData){
                if (Config.ERROR_OK == resData.data.code) {
                    resolve(resData)                
                }else {
                    reject(resData)
                }
            },
            fail(error){
                reject(error)
            }
        })
    })
}

/**
 * 获取用户信息
 */
export const getMemberDetail = (data) => {
    return new Promise((resolve, reject) => {
        getApp().sendRequest({
            url: Api.GET_MEMBERDETAIL,
            data: data,
            method: "POST",
            success(resData) {
                if (Config.ERROR_OK == resData.data.code) {
                    resolve(resData)
                } else {
                    reject(resData)
                }
            },
            fail(error) {
                reject(error)
            }
        })
    })
}


/**
 * 获取邀请信息
 */
export const getAppInviteCodeImage = (data) => {
    const handledData = Object.assign({
        method: 'getAppInviteCodeImage.do',
    }, data)
    return new Promise((resolve, reject) => {
        getApp().sendRequest({
            url: Api.GET_APPINVITECODEIMAGE,
            data: handledData,
            method: "POST",
            success(resData) {
                if (Config.ERROR_OK == resData.data.code) {
                    resolve(resData.data.data)
                } else {
                    reject(resData)
                }
            },
            fail(error) {
                reject(error)
            }
        })
    })
}